import { ID } from '@/types'
import { request } from '@/utils/request'

export function getQueryCourses (query: {
  'currentPage': number;
  'pageSize': number;
  'courseName': string;
  'status': number | string;
}) {
  return request.post('/boss/course/getQueryCourses', query)
}

export function getCourseById (courseId: number | string) {
  return request.get('/boss/course/getCourseById', { params: { courseId } })
}

export function getSectionById (sectionId: number | string) {
  return request.get('/boss/course/section/getBySectionId', { params: { sectionId } })
}

export function getLessonById (lessonId: number | string) {
  return request.get('/boss/course/lesson/getById', { params: { lessonId } })
}
export function getSectionAndLesson (courseId: number | string) {
  return request.get('/boss/course/section/getSectionAndLesson', { params: { courseId } })
}

export function changeState (params: {
  'courseId': ID;
  'status': number | string;
}) {
  return request.get('/boss/course/changeState', { params })
}

export function upload (file: Blob, onUploadProgress?: (progressEvent: any) => void) {
  const formData = new FormData()
  formData.append('file', file)
  return request.post('/boss/course/upload', formData, { onUploadProgress })
}

export interface CourseUpdateData {
  'id': ID;
  'courseName': string;
  'brief': string;
  'teacherDTO': {
    'id': ID;
    'courseId': ID;
    'teacherName': string;
    // 'teacherHeadPicUrl': string;
    'position': string;
    'description': string;
  };
  'courseDescriptionMarkDown': string;
  'price': number;
  'discounts': number;
  'priceTag': string;
  'discountsTag': string;
  // 'isNew': true;
  // 'isNewDes': string;
  'courseListImg': string;
  'courseImgUrl': string;
  'sortNum': number;
  'previewFirstField': string;
  'previewSecondField': string;
  'status': number;
  'sales': number;
  'activityCourse': boolean;
  'activityCourseDTO': {
    'id'?: number;
    // 'courseId': number;
    'beginTime': string;
    'endTime': string;
    'amount': number;
    'stock': number;
  };
  // 'autoOnlineTime': string;
}

export function saveOrUpdateCourse (data: CourseUpdateData) {
  return request.post('/boss/course/saveOrUpdateCourse', data)
}

// {"brief":"test_lnw_course_brief","courseDescriptionMarkDown":"<p>markdown</p>\n","courseImgUrl":"","courseListImg":"","courseName":"test_lnw_course_1","discounts":"222","discountsTag":"","id":"","previewFirstField":"test_lnw_course_b1","previewSecondField":"test_lnw_course_b2","price":"","priceTag":"","sales":"","sortNum":"-12","status":"","teacherDTO":{"teacherName":"test_lnw_course_t","position":"test_lnw_course_t_p","description":"test_lnw_course_t_d"},"activityCourse":true,"activityCourseDTO":{"beginTime":"2020-12-14T04:00:00.000Z","endTime":"2020-12-17T04:00:00.000Z","amount":"33","stock":"5"}}

export function saveOrUpdateSection (data: {
  'id'?: ID;
  'courseId'?: ID;
  'courseName'?: string;
  'sectionName'?: string;
  'description'?: string;
  'orderNum'?: ID;
  'status'?: ID;
}) {
  return request.post('/boss/course/section/saveOrUpdateSection', data)
}
export function saveOrUpdateLesson (data: {
  'id'?: ID;
  'courseId'?: ID;
  'sectionId'?: ID;
  'theme'?: string;
  'duration'?: ID;
  'isFree'?: boolean;
  'orderNum'?: ID;
  'status'?: ID;
}) {
  return request.post('/boss/course/lesson/saveOrUpdate', data)
}

export function getAliyunImagUploadAddressAdnAuth () {
  return request.get('/boss/course/upload/aliyunImagUploadAddressAdnAuth.json')
}

export function getAliyunVideoUploadAddressAdnAuth (fileName: string, imageUrl: string) {
  return request.get('/boss/course/upload/aliyunVideoUploadAddressAdnAuth.json', { params: { fileName, imageUrl } })
}

export function postAliyunTransCode (data: { sectionId: ID; lessonId: ID; fileId: ID; fileName: string }) {
  return request.post('/boss/course/upload/aliyunTransCode.json', data)
}
export function getAliyunTransCodePercent (lessonId: ID) {
  return request.get('/boss/course/upload/aliyunTransCodePercent.json', {
    params: {
      lessonId
    }
  })
}
